<?php
$_setting_user = new user_controller();
if(!isset($_GET['action'])) header("Location:../backend/index.php?action=user&page=1&row=5");
if (isset($_GET['do']) && $_GET['do'] == 'del') {
    $id = $_GET['id'];
    $del_acc = $_setting_user->deleteAcountbyId($id);
}
if(isset($_GET['action']) && ((isset($_GET['do'])) && ($_GET['do']=='add') || ($_GET['do']=='edit')))
{
	$width = 0;$color = ""; $message = "";
	$userInfo = "*";
	$passInfo = "*";
	$fnameInfo = "*";
	$lnameInfo = "*";
	$emailInfo = "*";
    $uservalue = "";
	$fnamevalue = "";
	$lnamevalue = "";
	$wnamevalue = "";
	$urlvalue = "";
	$emailvalue = "";
    $location = 'Argentina';
	$d = 1;
	$m = 1;
	$y = 1970;
	$male = "";
	$female = "";
	$admin = "";
	$member = "";
    $location = "Argentina";
	$class_input_user = "inp-form";
	$class_input_pass = "inp-form";
	$class_input_fname = "inp-form";
	$class_input_lname = "inp-form";
	$class_input_email = "inp-form";
	$class_input_wname = "inp-form";
	$class_input_url = "inp-form";
    $disable_input_user = "";
	if ($_GET['do'] == 'add')$page_heading = "Add New User"; else $page_heading = "Edit User";
	if(isset($_POST['submit'])){
		$width = 100;
		if($_GET['do'] == 'add'){
			if ((!validateUser($_POST['txt_username'])) || (!validatePass($_POST['txt_password'])) || (!$_setting_user->checkEmail($_POST['txt_email'])) || (!$_setting_user->checkUser($_POST['txt_username']))
			|| (!validateFirstname($_POST['txt_firstname'])) || (!validateLastname($_POST['txt_lastname'])) || (!$_setting_user->isValidEmail($_POST['txt_email']))){
						$color = "red";
						$message = "Add New Account Failed !";
						$uservalue = $_POST['txt_username'];
						$fnamevalue = $_POST['txt_firstname'];
						$lnamevalue = $_POST['txt_lastname'];
						$wnamevalue = $_POST['txt_website'];
						$urlvalue = $_POST['txt_siteurl'];
						$emailvalue = $_POST['txt_email'];
                        $location = $_POST['txt_location'];
                        if($_POST['txt_sex']== 'male') $male = "selected='selected'"; else $female = "selected='selected'";
						if ($_POST['role'] == 1) $admin = "selected='selected'";  else $member = "selected='selected'";
						$y = $_POST['y'];$m = $_POST['m'];$d = $_POST['d'];
						if ($_GET['do'] == 'add') $message = "Add New Account Failed !"; else $message = "Edit Account Failed !";
						if(!validateUser($_POST['txt_username'])){ $userInfo = "Username must more than 4 letters !"; $class_input_user = "inp-form-error"; }
						if(!validatePass($_POST['txt_password'])){ $passInfo = "Password must more than 6 letters !"; $class_input_pass = "inp-form-error"; }
						if(!validateFirstname($_POST['txt_firstname'])){ $fnameInfo = "Firstname is not null !";$class_input_fname = "inp-form-error"; }
						if(!validateLastname($_POST['txt_lastname'])){ $lnameInfo = "Lastname is not null !";$class_input_lname = "inp-form-error"; }
						if(!$_setting_user->isValidEmail($_POST['txt_email'])){ $emailInfo = "Type a valid e-mail please :P";$class_input_email = "inp-form-error"; }
                        elseif (!$_setting_user->checkEmail($_POST['txt_email'])){$emailInfo = "Email is ready exits !";$class_input_email = "inp-form-error";}
						if(!$_setting_user->checkUser($_POST['txt_username'])){$userInfo = "Username is ready exits !"; $class_input_user = "inp-form-error";}
					} else {
						$color = "green";
						$birthday = $_POST['y'] . '-' . $_POST['m'] . '-' . $_POST['d'];
						$account = new users('', $_POST['txt_username'], $_POST['txt_password'], $_POST['txt_firstname'], $_POST['txt_lastname'], $_POST['txt_sex'], $_POST['txt_email'], $birthday, $_POST['txt_website'], $_POST['txt_url'], $_POST['role'],$_POST['txt_location']);
						if($_setting_user->addAcount($account))$message = "Add New Account Successfully !";		
					}
		}elseif ($_GET['do'] == 'edit'){
			if((!validateFirstname($_POST['txt_firstname'])) || (!validateLastname($_POST['txt_lastname']))	|| (!$_setting_user->isValidEmail($_POST['txt_email'])) || (!$_setting_user->checkEmailExceptId($_POST['txt_email'],$_GET['id'])) || ((!validatePass($_POST['txt_password'])) && ($_POST['txt_password'] != ''))){
						$color = "red";
                        $disable_input_user = "disabled='disabled'";
                        $id = $_GET['id'];
                        $stus = $_setting_user->getAccountById($id);
						$uservalue = $stus->username;
						$fnamevalue = $_POST['txt_firstname'];
						$lnamevalue = $_POST['txt_lastname'];
						$wnamevalue = $_POST['txt_website'];
						$urlvalue = $_POST['txt_siteurl'];
						$emailvalue = $_POST['txt_email'];
                        $location = $_POST['txt_location'];
                        if($_POST['txt_sex']== 'male') $male = "selected='selected'"; else $female = "selected='selected'";
						if ($_POST['role'] == 1) $admin = "selected='selected'";  else $member = "selected='selected'";
						$y = $_POST['y'];$m = $_POST['m'];$d = $_POST['d'];
						$message = "Edit Account Failed !";
                        if($_POST['txt_password']!= "") { $passInfo = "Password must more than 6 letters !";$class_input_pass = "inp-form-error"; }
						if(!validateFirstname($_POST['txt_firstname'])){ $fnameInfo = "Firstname is not null !";$class_input_fname = "inp-form-error"; }	
						if(!validateLastname($_POST['txt_lastname'])){ $lnameInfo = "Lastname is not null !";$class_input_lname = "inp-form-error"; }
						if(!$_setting_user->checkEmailExceptId($_POST['txt_email'],$_GET['id'])){ $emailInfo = "Email is ready exits !";$class_input_email = "inp-form-error";}
						if(!$_setting_user->isValidEmail($_POST['txt_email'])){ $emailInfo = "Type a valid e-mail please :P";$class_input_email = "inp-form-error"; }
			}else{
                        $disable_input_user = "disabled='disabled'";
						$color = "green";
						$pass = $_POST['txt_password'];
						$birthday = $_POST['y'] . '-' . $_POST['m'] . '-' . $_POST['d'];
						$id = $_GET['id'];
						$account = new users($id, $_POST['txt_username'], $_POST['txt_password'], $_POST['txt_firstname'], $_POST['txt_lastname'], $_POST['txt_sex'], $_POST['txt_email'], $birthday, $_POST['txt_website'], $_POST['txt_url'], $_POST['role'],$_POST['txt_location']);
						if ($pass != "") {
							$_setting_user->editAcount($account);
						} else {
							$_setting_user->editAcountNotPass($account);
						}
						$message = "Edit Account Successfully";
						$stus = $_setting_user->getAccountById($id);
						$y = substr($stus->birthday, 0, 4);
						$m = substr($stus->birthday, 5, 2);
						$d = substr($stus->birthday, 8, 2);
						if($stus->sex == 'male') $male = "selected='selected'"; else $female = "selected='selected'";
						if ($stus->role == 1) $admin = "selected='selected'";  else $member = "selected='selected'";
						$uservalue = $stus->username;
						$fnamevalue = $stus->firstname; 
						$lnamevalue = $stus->lastname; 
						$emailvalue = $stus->email;
						$wnamevalue = $stus->sitename;
						$urlvalue = $stus->siteurl;
                        $location = $stus->location;
			}
		}
	}else{
        if($_GET['do'] == 'edit') {
        $disable_input_user = "disabled='disabled'";
		$id = $_GET['id'];
		$stus = $_setting_user->getAccountById($id);
		$y = substr($stus->birthday, 0, 4);
        $m = substr($stus->birthday, 5, 2);
        $d = substr($stus->birthday, 8, 2);
		if($stus->sex == 'male') $male = "selected='selected'"; else $female = "selected='selected'";
		if ($stus->role == 1) $admin = "selected='selected'";  else $member = "selected='selected'";
		$uservalue = $stus->username;
		$fnamevalue = $stus->firstname; 
		$lnamevalue = $stus->lastname; 
		$emailvalue = $stus->email;
		$wnamevalue = $stus->sitename;
		$urlvalue = $stus->siteurl;
        $location = $stus->location;
		}
	}
?>
    <div id="content-outer">
        <!-- start content -->
        <div id="content">
            <div id="page-heading"><h1><?php echo $page_heading?></h1></div>
            <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                <tr>
                    <th rowspan="3" class="sized"><img src="images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                    <th class="topleft"></th>
                    <td id="tbl-border-top">&nbsp;</td>
                    <th class="topright"></th>
                    <th rowspan="3" class="sized"><img src="images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                </tr>
                <tr>
                    <td id="tbl-border-left"></td>
                    <td>
                        <!--  start content-table-inner -->
                        <div id="content-table-inner">
							<div id="message-<?php echo $color?>">
								<table border="0" width="<?php echo $width."%"?>" cellpadding="0" cellspacing="0">
									<tr>
										<td class="<?php echo $color?>-left"><?php echo $message?></td>
										<td class="<?php echo $color?>-right">
											<a class="close-<?php echo $color?>"><img src="../images/table/icon_close_<?php echo $color?>.gif"   alt="" />											</a>
										</td>
										</tr>
								</table>
							</div>  
                            <form action="" method="POST">
                                <table border="0" width="100%" cellpadding="0" cellspacing="0">
                                    <tr valign="top">
                                        <td>
                                            <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
                                                <tr>
                                                    <th valign="top">Username :</th>
                                                    <td>
														<input id="txt_username" name="txt_username" <?php echo $disable_input_user ?> type="text" class="<?php echo $class_input_user?>" value="<?php echo $uservalue?>" />
														<span id="userInfo" class="error"><?php echo $userInfo?></span>
													</td>
                                                </tr>
                                                <tr>
                                                    <th valign="top">Password :</th>
                                                    <td>
														<input id="txt_password" type="password" name="txt_password" class="<?php echo $class_input_pass?>" />
														<span id="passInfo" class="error"><?php echo $passInfo?></span>
													</td>
                                                </tr>
                                                <tr>
                                                    <th valign="top">Firstname :</th>
                                                    <td>
														<input id="txt_firstname" name="txt_firstname" type="text" class="<?php echo $class_input_fname?>" value="<?php echo $fnamevalue?>" />
														<span id="fnameInfo" class="error"><?php echo $fnameInfo?></span>	
													</td>
                                                </tr>
                                                <tr>
                                                    <th valign="top">Lastname :</th>
                                                    <td>
														<input id="txt_lastname" type="text" name="txt_lastname" class="<?php echo $class_input_lname?>" value="<?php echo $lnamevalue?>" />
														<span id="lnameInfo" class="error"><?php echo $lnameInfo?></span>
													</td>
                                                </tr>
                                                <tr>
                                                    <th valign="top">Sex :</th>
                                                    <td>
                                                        <select name="txt_sex" class="styledselect-day">
                                                            <option value="male" <?php echo $male ?> > Male </option>
                                                            <option value="female" <?php echo $female ?> > Female </option>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <th valign="top">Email :</th>
                                                    <td>
														<input id="txt_email" type="text" name="txt_email" class="<?php echo $class_input_email?>" value="<?php echo $emailvalue?>"/>
														<span id="emailInfo" class="error"><?php echo $emailInfo?></span>
													</td>
                                                </tr>
                                                <tr id="location-box">
                                                    <th valign="top">Location :</th>
                                                    <td>
                                                        <select id="txt_location" name="txt_location" class="text" >
                                                        <?php
                                                            $mo = array("Argentina","Austria","Australia","Angola","Bolivia","Brazil","Canada","China","Chile","Colombia","Cuba","Denmark","England","Ecuador","Egypt","France","Germany","Haiti","Indonesia","Jamaica","Japan","Korea","Malaysia","Mexico","Paraguay","Peru","Singapore","Russia","Turkey","Thailand","Venezuela","Vietnam","Uruguay","USA");
															for($n=0;$n<count($mo);$n++)if($mo[$n] == $location) echo  "<option value='".$mo[$n]."' selected='selected'>".$mo[$n]."</option>";else echo "<option value='".$mo[$n]."'>".$mo[$n]."</option>";
                                                         ?>      
                                                        </select>
													</td>
                                                </tr>
                                                <tr>
                                                    <th valign="top">Birthday :</th>
                                                    <td class="noheight">
                                                        <table border="0" cellpadding="0" cellspacing="0">
                                                            <tr  valign="top">
                                                                <td>
                                                                    <form id="chooseDateForm" action="#">
                                                                        <select id="d" name="d" class="styledselect-day">
																			<?php for($n=1;$n<32;$n++) if($n == $d) echo  "<option value='".$n."' selected='selected'>".$n."</option>"; else echo "<option value='".$n."'>".$n."</option>";?>
                                                                        </select>
                                                                </td>
                                                                <td>
                                                                    <select id="m" name="m" class="styledselect-month">
																		<?php 
																			$mo = array("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
																			for($n=1;$n<=12;$n++)if($n == $m) echo  "<option value='".$n."' selected='selected'>".$mo[$n]."</option>";else echo "<option value='".$n."'>".$mo[$n]."</option>";
																		?>
                                                                    </select>
                                                                </td>
                                                                <td>
                                                                    <select  id="y" name="y"  class="styledselect-year">
																		<?php for($n=1970;$n<=1995;$n++)if($n == $y) echo  "<option value='".$n."' selected='selected'>".$n."</option>";else  echo "<option value='".$n."'>".$n."</option>";?>
                                                                    </select>
                                                                    </form>
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <th valign="top">Website Name :</th>
                                                    <td><input type="text" name="txt_website" class="<?php echo $class_input_wname?>" value="<?php echo $wnamevalue?>" /></td>
                                                    <td></td>
                                                </tr> 
                                                <tr>
                                                    <th valign="top">URL Site :</th>
                                                    <td><input name="txt_url" type="text" class="<?php echo $class_input_url?>" value="<?php echo $urlvalue?>" /></td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <th valign="top">Select Role:</th>
                                                    <td class="noheight">
                                                        <select name="role" class="styledselect-day">
                                                            <option value="1" <?php echo $admin ?> > Admin </option>
                                                            <option value="2" <?php echo $member ?> > Member </option>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <th>&nbsp;</th>
                                                    <td valign="top">
                                                        <input type="submit" name="submit" value="submit" class="form-submit" />
														<a onclick="location='index.php?action=user&page=<?php echo $_SESSION['page']+1?>&row=<?php echo $_SESSION['row']?>'" class="form-cancel">Cancel</a>
                                                        <input type="reset" value="" class="form-reset"  />
                                                    </td>
                                                    <td></td>
                                                </tr>
                                            </table>
                                            <!-- end id-form  -->
                                    <tr>
                                        <td><img src="../images/shared/blank.gif" width="695" height="1" alt="blank" /></td>
                                    </tr>
                                </table>
                            </form>
                            <div class="clear"></div>
                        </div>
                        <!--  end content-table-inner  -->
                    </td>
                    <td id="tbl-border-right"></td>
                </tr>
                <tr>
                    <th class="sized bottomleft"></th>
                    <td id="tbl-border-bottom">&nbsp;</td>
                    <th class="sized bottomright"></th>
                </tr>
            </table>
            <div class="clear">&nbsp;</div>
        </div>
        <!--  end content -->
        <div class="clear">&nbsp;</div>
    </div>
    <?php } else {
            $_SESSION['page']=$_GET['page']-1;
            $_SESSION['row']=$_GET['row'];
    ?>
    <div id="content-outer">
        <!-- start content -->
        <div id="content">

            <!--  start page-heading -->
            <div id="page-heading">
                <h1><a style="color: #2e74b2; cursor: pointer;" onclick="location='index.php?action=user&do=add'">Add Account</a></h1>
            </div>
            <!-- end page-heading -->

            <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                <tr>
                    <th rowspan="3" class="sized"><img src="../images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                    <th class="topleft"></th>
                    <td id="tbl-border-top">&nbsp;</td>
                    <th class="topright"></th>
                    <th rowspan="3" class="sized"><img src="../images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                </tr>
                <tr>
                    <td id="tbl-border-left"></td>
                    <td>
                        <!--  start content-table-inner ...................................................................... START -->
                        <div id="content-table-inner">
                            <!--  start table-content  -->
                            <div id="table-content">

                                <!--  start product-table ..................................................................................... -->
                                <form id="mainform" action="">
                                    <table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
                                        <tr>
                                            <th class="table-header-check"><a id="toggle-all" ></a> </th>
                                            <th class="table-header-repeat line-left minwidth-1"><a href="">Username </a>	</th>
                                            <th class="table-header-repeat line-left minwidth-1"><a href="">Email </a></th>
                                            <th class="table-header-repeat line-left"><a href="">Website Name </a></th>
                                            <th class="table-header-repeat line-left"><a href="">URL Site </a></th>
                                            <th class="table-header-repeat line-left"><a href="">Role </a></th>
                                            <th class="table-header-options line-left"><a href="">Options</a></th>
                                        </tr>
								<?php
                                $page = $_GET['page']-1;
                                $row = $_GET['row'];
                                $lastpage = (int)(($_setting_user->countPageUser()-1)/$_GET['row'])+1;
                                if($page>=$lastpage) header("Location:../backend/index.php?action=user&page=1&row=".$row);
								$stus = $_setting_user->pagingUser($page,$_GET['row']);
								foreach ($stus as $stusk) {
									?>
                                            <form action="" method="POST">
                                                <tr>
                                                    <td><input type="hidden" name="id" value="<?php echo $stusk->id; ?>"><input  type="checkbox"/></td>

                                                    <td><?php echo $stusk->username; ?></td>
                                                    <td><?php echo $stusk->email; ?></td>
                                                    <td><?php echo $stusk->sitename; ?></td>
                                                    <td><?php echo $stusk->siteurl; ?></td>
                                                    <td><?php
														if ($stusk->role == 1) {
															echo 'Admin';
														} elseif ($stusk->role == 2) {
															echo 'Member';
														}?>
													</td>
                                                    <td>
                                                        <a title="Edit" onclick="location='index.php?action=user&do=edit&id=<?php echo $stusk->id; ?>'" class="icon-1 info-tooltip"></a>
                                                        <a title="Delete" onclick="location='index.php?action=user&do=del&id=<?php echo $stusk->id; ?>&page=<?php echo $page+1?>&row=<?php echo $row?>'" class="icon-2 info-tooltip"></a>
                                                    </td>
                                                </tr> 
                                            </form>
								<?php
							}
							?>
                                    </table>
                                    <!--  end product-table................................... --> 
                                </form>
                            </div>
						<table border="0" cellpadding="0" cellspacing="0" id="paging-table">
							<tr>
							<td>
								<a onclick="location='index.php?action=user&page=1&row=<?php echo $row?>'" class="page-far-left"></a>
								<a onclick="location='index.php?action=user&page=<?php if($page>0) echo $page; else echo 1;?>&row=<?php echo $row?>'" class="page-left"></a>
								<div id="page-info">Page <strong id="page-current"><?php echo $page+1 ?></strong> / <?php echo $lastpage;?></div>
								<a onclick="location='index.php?action=user&page=<?php if($page+2<$lastpage) echo $page+2; else echo $lastpage;?>&row=<?php echo $row?>'" class="page-right"></a>
								<a onclick="location='index.php?action=user&page=<?php echo $lastpage;?>&row=<?php echo $row?>'" class="page-far-right"></a>
							</td>
							<td>
							<select  class="styledselect_pages">
								<option value="">Number of rows</option>
								<option value="">5</option>
								<option value="">10</option>
								<option value="">15</option>
							</select>
							</td>
							</tr>
						</table>
                            <!--  end paging................ -->
                            <div class="clear"></div>
                        </div>
                        <!--  end content-table-inner ............................................END  -->
                    </td>
                    <td id="tbl-border-right"></td>
                </tr>

            </table>
            <div class="clear">&nbsp;</div>
        </div>
        <!--  end content -->
        <div class="clear">&nbsp;</div>
    </div>
    <?php
}
?>
<script type="text/javascript" src="../js/validuser.js"></script>